package com.example.ssm.controller;

import com.example.ssm.pojo.User;
import com.example.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @author zhangdongsheng
 * @version 1.0
 * @describe
 * @date 2021/2/20 21:27
 */
@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    //登录功能
    public  String login(HttpServletRequest httpServletRequest, HttpSession session, HttpServletResponse httpServletResponse, Map<String,Object> map)throws Exception{
        String username = httpServletRequest.getParameter( "username" );
        String password = httpServletRequest.getParameter( "password" );
        User user = userService.findByUsernameAndPassword(username, password);
        session.setAttribute( "username",username);
        if (user!=null){
            httpServletResponse.setContentType("text/html;charset=utf-8");
            //将信息返回
            PrintWriter out = httpServletResponse.getWriter();
            out.print( "<script type=\"text/javascript\">alert('登陆成功!!!')</script>" );
            out.close();
//            return "person";
            return "success";
        } else {
             //弹出窗口提示登录失败
             httpServletResponse.setContentType("text/html;charset=utf-8");
             PrintWriter out1 = httpServletResponse.getWriter();
             out1.print( "<script type=\"text/javascript\">alert('登陆失败,请检查用户名和密码!!!')</script>" );
            return "login";
        }

    }
}
